import {compare, isInArray} from '@/utils'
// link扩展
export default class LinkExtension extends StructureLink {

    /* 任务系统3.0 链接处理任务逻辑 */
    public HandMisson():void{
        if (!this.room.memory.Task['STRUCTURE']) this.room.memory.Task['STRUCTURE'] = []
        var allmyTask = []
        for (var task of this.room.memory.Task['STRUCTURE'])
        {
            if (isInArray(task.relateStructure,this.id))
            {
                allmyTask.push(task)
            }
        }
        /* 按照优先级排序 */
        if (allmyTask.length <= 0) return
        else if (allmyTask.length > 1)
            allmyTask.sort(compare('taskLevel'))
        /* 处理任务 */
        let thisTask = allmyTask[0]
        if (thisTask.delayTick < 99995)
            thisTask.delayTick--
        switch (thisTask.Name){
            case "链传送能":{this.HandleLToL();break}
        }
    }

    /* 链传送能 */
    public HandleLToL():void{
        if (this.cooldown && this.cooldown > 0) return
        var myTask:RoomTask
        LoopLink:
        for (var i of this.room.memory.Task['STRUCTURE'])
        {
            if (isInArray(i.relateStructure,this.id))
            {
                myTask = i
                break LoopLink
            }
        }
        if (!myTask) return
        var dis = Game.getObjectById(myTask.disStructure[0]) as StructureLink
        if (!dis) return
        var result = this.transferEnergy(dis)
        if (result == OK || result == ERR_FULL)
        {
            this.room.deleteMisson_ID(myTask.ID)
        }
    }

}